﻿using System.Collections.Generic;
using System.Linq;
using Foothill.Common;
using Foothill.Model;

namespace Foothill.Service.AdService
{
    public class ReorderService : IReorderService
    {
        public OrderByItems GetOrderByItem(string orderByItemName)
        {
            orderByItemName = orderByItemName.ToLower();

            switch (orderByItemName)
            {
                case "price":
                    return OrderByItems.ByPrice;
                case "mileage":
                    return OrderByItems.ByKilometers;
                case "year":
                    return OrderByItems.ByYearAge;
                case "lastedittime":
                    return OrderByItems.ByLastEditTime;
                case "model":
                    return OrderByItems.ByModel;
            }

            return OrderByItems.ByPrice;
        }

        public IList<ForSaleAd> ReorderSearchResultList(OrderByItems orderByItem, fhSortDirection direction, IList<ForSaleAd> displayResultList)
        {
            if (direction == fhSortDirection.ASC)
            {
                return displayResultList.OrderBy(c => GetOrderByProperty(orderByItem, c)).ToList();
            }

            return displayResultList.OrderByDescending(c => GetOrderByProperty(orderByItem, c)).ToList();
        }

        public IDictionary<int, string> GetOrderByListItems()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            result.Add((int)OrderByItems.ByKilometers, "公里数");
            result.Add((int)OrderByItems.ByPrice, "价格");
            result.Add((int)OrderByItems.ByYearAge, "车龄");
            
            return result;
        }

        private object GetOrderByProperty(OrderByItems orderByItem, ForSaleAd post)
        {
            switch (orderByItem)
            {
                case OrderByItems.ByPrice:
                    return post.Price;
                case OrderByItems.ByKilometers:
                    return post.MileAge;
                case OrderByItems.ByYearAge: 
                    return 0 - post.YearMade;
                case OrderByItems.ByLastEditTime:
                    return post.LastEditTime;
                case OrderByItems.ByModel:
                    return post.ModelId;
            }

            return post.Price;
        }
    }
}
